package com.oc.ags.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oc.ags.domain.TruckDO;
import com.oc.ags.service.TruckService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author errorist
 * @email it@gznict.com
 * @date 2018-05-17 15:24:22
 */
 
@Controller
@RequestMapping("/ags/truck")
public class TruckController {
	@Autowired
	private TruckService truckService;
	
	@GetMapping()
	@RequiresPermissions("ags:truck:truck")
	String Truck(){
	    return "ags/truck/truck";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("ags:truck:truck")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<TruckDO> truckList = truckService.list(query);
		int total = truckService.count(query);
		PageUtils pageUtils = new PageUtils(truckList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("ags:truck:add")
	String add(){
	    return "ags/truck/add";
	}

	@GetMapping("/edit/{truckNo}")
	@RequiresPermissions("ags:truck:edit")
	String edit(@PathVariable("truckNo") String truckNo,Model model){
		TruckDO truck = truckService.get(truckNo);
		model.addAttribute("truck", truck);
	    return "ags/truck/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("ags:truck:add")
	public R save( TruckDO truck){
		if(truckService.save(truck)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("ags:truck:edit")
	public R update( TruckDO truck){
		truckService.update(truck);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("ags:truck:remove")
	public R remove( String truckNo){
		if(truckService.remove(truckNo)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("ags:truck:batchRemove")
	public R remove(@RequestParam("ids[]") String[] truckNos){
		truckService.batchRemove(truckNos);
		return R.ok();
	}
	
}
